package sz.szsmk.citizencard.ui.adapter;

import android.os.Bundle;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.DefaultLayoutHelper;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.RoutineOneBinding;
import sz.szsmk.citizencard.model.network.bean.response.QueryUserCardResponse;
import sz.szsmk.citizencard.ui.activity.HomeRoutineCardActivity;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.JumpUtils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/16 16:56
 * description:
 */
public class RoutineOneAdapter extends DelegateAdapter.Adapter<RoutineOneAdapter.RoutineOneHolder> {
    private HomeRoutineCardActivity mContext;
    private QueryUserCardResponse.DataBean xxBean;
    private QueryUserCardResponse.DataBean ylBean;

    private LayoutHelper mDefaultLayoutHelper;
    private QueryUserCardResponse.DataBean qzwwkData;
    private String access_token;

    public RoutineOneAdapter(HomeRoutineCardActivity homeRoutineCardActivity, DefaultLayoutHelper oneHelper) {
        mContext = homeRoutineCardActivity;
        mDefaultLayoutHelper = oneHelper;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mDefaultLayoutHelper;
    }

    @Override
    public int getItemViewType(int position) {
        return R.layout.routine_one;
    }

    @Override
    public RoutineOneHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return RoutineOneHolder.creat(parent);
    }

    @Override
    public void onBindViewHolder(RoutineOneHolder holder, int position) {
        loadItemData(position, holder.binding);
        // mContext.finish();
    }

    private void loadItemData(int position, RoutineOneBinding binding) {
        if (xxBean != null) {
            if (xxBean.getCardState()!=null){
                if (xxBean.getCardState().equals("1")) {
                    binding.cardXxnkWkt.setVisibility(View.GONE);
                    binding.cardXxnkYkt.setVisibility(View.VISIBLE);
                    binding.cardEndtimesXx.setText(xxBean.getEndTime() + "");
                    binding.cardTimesXx.setText(xxBean.getSpareTimes() + "次");
                } else {
                    binding.cardXxnkWkt.setVisibility(View.VISIBLE);
                    binding.cardXxnkYkt.setVisibility(View.GONE);
                }
            }

        }
        if (ylBean != null) {
            if (ylBean.getCardState().equals("1")) {
                binding.cardYlnkWkt.setVisibility(View.GONE);
                binding.cardYlnkYkt.setVisibility(View.VISIBLE);
                binding.cardYlnkEndtimes.setText(xxBean.getEndTime() + "");
                binding.cardYlnkTimes.setText(xxBean.getSpareTimes() + "次");
            } else {
                binding.cardYlnkWkt.setVisibility(View.VISIBLE);
                binding.cardYlnkYkt.setVisibility(View.GONE);
            }

        }
        if (qzwwkData != null) {
            if (qzwwkData.getCardState().equals("1")) {
                binding.cardQzwwkWkt.setVisibility(View.GONE);
                binding.cardQzwwkYkt.setVisibility(View.VISIBLE);
                binding.cardQzwwkEndtimes.setText(xxBean.getEndTime() + "");
                binding.cardQzwwkTimes.setText(xxBean.getSpareTimes() + "次");
            } else {
                binding.cardQzwwkWkt.setVisibility(View.VISIBLE);
                binding.cardQzwwkYkt.setVisibility(View.GONE);
            }

        }

        access_token = ACache.get(mContext).getAsString("access_token");
        binding.cardXxnkLjbl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = Constant.BASE_URL + "app-front-end/#/card/main?access_token=" + access_token;
                Bundle bundle = new Bundle();
                bundle.putString("url", url);
                JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);
            }
        });
        binding.cardYlnkLjbl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = Constant.BASE_URL + "app-front-end/#/gardenCard/main?access_token==" + access_token;
                Bundle bundle = new Bundle();
                bundle.putString("url", url);
                JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);
            }
        });
        binding.cardQzwwkLjbl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = Constant.BASE_URL + "app-front-end/#/familycard/main";
                Bundle bundle = new Bundle();
                bundle.putString("url", url);
                JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);
            }
        });
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    public void setHuiminData(QueryUserCardResponse.DataBean huiminData) {
        xxBean = huiminData;
    }

    public void setYldata(QueryUserCardResponse.DataBean ylBean) {
        this.ylBean = ylBean;
    }

    public void setQzwwkData(QueryUserCardResponse.DataBean qzwwkData) {
        this.qzwwkData = qzwwkData;
    }

    public static class RoutineOneHolder extends RecyclerView.ViewHolder {

        private RoutineOneBinding binding;

        public static RoutineOneHolder creat(ViewGroup parent) {
            RoutineOneBinding binding = RoutineOneBinding
                    .inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new RoutineOneHolder(binding);
        }

        public RoutineOneHolder(RoutineOneBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
